package grapid.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * 文本类
 * 
 * @author grapid
 * @version 2.0
 * 
 */
public class GText {

	/**
	 * KeywordBean
	 * 
	 * @author grapid
	 * 
	 */
	public class KeywordBean {
		private String keyword; // 正则匹配关键字
		private String replacement; // 被替换文本
		private boolean isReplace = true; // 是否替换
		private boolean isBreak = false; // 替换后是否中断执行

		public String getKeyword() {
			return keyword;
		}

		public void setKeyword(String keyword) {
			this.keyword = keyword;
		}

		public boolean isBreak() {
			return isBreak;
		}

		public void setBreak(boolean isBreak) {
			this.isBreak = isBreak;
		}

		public String getReplacement() {
			return replacement == null ? "" : replacement;
		}

		public void setReplacement(String replacement) {
			this.replacement = replacement;
		}

		public boolean isReplace() {
			return isReplace;
		}

		public void setReplace(boolean isReplace) {
			this.isReplace = isReplace;
		}
	}

	/**
	 * FileterKeywordResultBean
	 * 
	 * @author grapid
	 * 
	 */
	public class FileterKeywordResultBean {
		private String ctx; // 传入的文本
		private int num; // 查到的数量
		private List<String> keywordList = new ArrayList<String>(); // 检测到的关键字

		public void addKeyword(String keyword) {
			this.keywordList.add(keyword);
		}

		public String getKeyword(String splitMark) {
			String keywords = "";
			for (String keyword : this.keywordList) {
				keywords += splitMark + keyword;
			}
			return "".equals(keywords) ? "" : keywords.substring(splitMark.length());
		}

		// auto
		public String getCtx() {
			return ctx;
		}

		public void setCtx(String ctx) {
			this.ctx = ctx;
		}

		public int getNum() {
			return num;
		}

		public void setNum(int num) {
			this.num = num;
		}

		public List<String> getKeywordList() {
			return keywordList;
		}

		public void setKeywordList(List<String> keywordList) {
			this.keywordList = keywordList;
		}
	}

	/**
	 * 过滤 - 关键字
	 * 
	 * @param ctx
	 *            待过滤字符串
	 * @param keywordList
	 *            关键字列表
	 * @return FileterKeywordResultBean
	 */
	public FileterKeywordResultBean filterKeyword(String ctx, List<KeywordBean> keywordList) {
		int num = 0;
		FileterKeywordResultBean result = new FileterKeywordResultBean();
		if (ctx != null) {
			for (KeywordBean keywordBean : keywordList) {
				if (keywordBean != null && keywordBean.getKeyword() != null && !"".equals(keywordBean.getKeyword())) {
					if (ctx.matches(keywordBean.getKeyword())) {
						if (keywordBean.isReplace())
							ctx = ctx.replaceAll(keywordBean.getKeyword(), keywordBean.getReplacement());
						num++;
						result.addKeyword(keywordBean.getKeyword());
						if (keywordBean.isBreak())
							break;
					}
				}
			}
		}
		result.setCtx(ctx);
		result.setNum(num);
		return result;
	}

	/**
	 * 过滤 - HTML标签
	 * 
	 * @param ctx
	 *            待过滤字符串
	 * @return 返回过滤后字符串
	 */
	public static String filterHTML(String ctx) {
		return ctx.replaceAll("&", "&amp;").replaceAll("\"", "&quot;").replaceAll("<", "&lt;").replaceAll(">", "&gt;")
				.replaceAll(" ", "&nbsp;").replaceAll("\n", "<br />");
	}

	/**
	 * 过滤 - IFRAME标签
	 * 
	 * @param ctx
	 *            待过滤字符串
	 * @return 返回过滤后字符串
	 */
	public static String filterIframe(String ctx) {
		return ctx.replaceAll("(?i)<(/?iframe[^>]*?)>", "&lt;$1&gt;");
	}

	/**
	 * 过滤 - SCRIPT代码
	 * 
	 * @param ctx
	 *            待过滤字符串
	 * @return 返回过滤后字符串
	 */
	public static String filterScript(String ctx) {
		return ctx.replaceAll("(?i)<(/?script[^>]*?)>", "&lt;$1&gt;").replaceAll(
				"(?i)(<[^ ]*[^>]*)expression[^(]*\\([^>]*\\)([^>]*>)", "$1$2");
	}

	/**
	 * 产生随机长度字符串
	 * 
	 * @param strSeed
	 *            种子
	 * @param length
	 *            长度
	 * @return 返回随即字符串
	 */
	public static String random(String strSeed, int length) {
		String randomStr = "";
		if (length == 0) {
			return randomStr;
		}
		int seedLength = strSeed.length();
		java.util.Random r = new Random(new java.util.Date().getTime());
		for (int i = 0; i < length; i++) {
			int pos = r.nextInt(seedLength);
			randomStr = randomStr + strSeed.substring(pos, pos + 1);
		}
		return randomStr;
	}

	/**
	 * 产生随机长度字符串
	 * 
	 * @param length
	 *            长度
	 * @return 返回随即字符串
	 */
	public static String random(int length) {
		return random("ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890", length);
	}

	/**
	 * 判断字符串是否为空或null
	 * 
	 * @param str
	 *            待检查字符串
	 * @return 是否为空
	 */
	public static boolean isEmpty(String str) {
		return str == null || "".equals(str);
	}
}
